package com.lagou.edu.message.mq;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.common.constant.MQConstant;
import com.lagou.edu.common.mq.RocketMqService;
import com.lagou.edu.common.mq.dto.BaseMqDTO;
import com.lagou.edu.common.mq.listener.AbstractMqListener;
import com.lagou.edu.message.api.dto.CourseStatusReleaseDTO;
import com.lagou.edu.message.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author zlg
 * @create 2021-02-07 22:20
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = MQConstant.Topic.COURSE_STATUS_RELEASE, consumerGroup = "${rocketmq.producer.group}" + "_" + MQConstant.Topic.COURSE_STATUS_RELEASE)
public class CourseStatusReleaseListener extends AbstractMqListener<BaseMqDTO<CourseStatusReleaseDTO>> implements RocketMQListener<BaseMqDTO<CourseStatusReleaseDTO>> {
    
    @Autowired
    private IMessageService messageService;
    @Autowired
    private RocketMqService rocketMqService;
    
    @Override
    public void onMessage(BaseMqDTO<CourseStatusReleaseDTO> data) {
        log.info("data ===> {}", JSON.toJSONString(data));
        if (this.checkMessageId(data.getMessageId())) {
            return;
        }
        /*final List<Integer> userIdList = messageService.saveMessage(data.getData().getCourseId());
        if (!Collections.isEmpty(userIdList)) {
            rocketMqService.convertAndSend(MQConstant.Topic.COURSE_RELEASE_SEND_MESSAGE, new BaseMqDTO<List<Integer>>(userIdList, UUID.randomUUID().toString()));
        }*/
        rocketMqService.convertAndSend(MQConstant.Topic.COURSE_RELEASE_SEND_MESSAGE, new BaseMqDTO<Integer>(data.getData().getCourseId(), UUID.randomUUID().toString()));
        this.updateMessageId(data.getMessageId());
    }
    
}
